package com.atguigu.eduorder.service.impl;

import com.atguigu.eduorder.client.CourseOrderClient;
import com.atguigu.eduorder.client.MemberOrderClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.atguigu.service.entity.EduCourse;
import com.atguigu.service.entity.UcenterMember;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-09-17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private CourseOrderClient courseOrderClient;
    @Autowired
    private MemberOrderClient memberOrderClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        //根据课程id获取课程信息
        EduCourse courseDto = courseOrderClient.getCourse(courseId);
        //获取用户id获取信息
        UcenterMember member = memberOrderClient.getMember(memberId);

        //创建订单
        Order order=new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherId());
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(member.getMobile());
        order.setNickname(member.getNickname());
        order.setStatus(0);//是否付费，0未付费，1已付费
        order.setPayType(1);//设置支付类型，1为微信支付，2是支付宝支付
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
